﻿#region

using System;
using System.Collections.Generic;
using CherryTomato;
using CherryTomato.Entities;
using MediaSage.Lib.Configuration;
using MediaSage.Lib.Database;
using MediaSage.Lib.Utilities;

#endregion

namespace MediaSage.Lib.Plugin.RottenTomatosProvider
{
    public class RottenTomatosProvider : PluginBase
    {
        #region Fields

        private readonly Tomato api;
        private string API_Application = "MediaSage";
        private string API_Key = "vz5veu7a6kjxmmuuy58n9zdp";

        #endregion Fields

        #region Constructors

        public RottenTomatosProvider(Config Configuration, PluginEntity PE)
            : base(Configuration, PE)
        {
            api = new Tomato(API_Key);
        }

        #endregion Constructors

        #region Methods

        public override bool Run(MediaClass mediaObject)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public override IList<PersonEntity> SearchPersonByName(string name)
        {
            throw new NotImplementedException();
        }

        public override string[] About()
        {
            VersionInfo verInfo = GetVersionInfo();
            ;
            string[] returnValue =
                {
                    "RottenTomatosProvider v" + verInfo.MajorVersion + "." + verInfo.MinorVersion +
                    "." + verInfo.BuildNumber
                };
            return returnValue;
        }

        public override IList<MediaFileTypesEntity> ExtensionsHandled()
        {
            throw new NotImplementedException();
        }

        public override PluginEntity GetConfiguration()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///     We will use the RottenTomatos ID if there is one, otherwise we will search on IMDB ID
        /// </summary>
        /// <param name="inputMedia"> Some input MediaClass that has either a RottenTomatos ID or an IMDB ID </param>
        /// <returns> The data gleaned from RottenTomatos </returns>
        public override MediaClass GetMedia(MediaClass inputMedia, MediaMergeType mergeType)
        {
            logger.Log("Entering RottenTomatosProvider.GetMedia()");
            bool movieRetrieved = false;
            if (inputMedia.RottenTomatosID != null)
            {
                Movie movie = api.FindMovieById(inputMedia.RottenTomatosID);
                if (movie != null)
                {
                    movieRetrieved = true;
                    logger.Log(string.Format("Retrieved info for: {0} from Rotten Tomatos", movie.Title));
                    inputMedia = MergeResults(inputMedia, mergeType, movie);
                }
            }

            return new MediaClass(config);
        }

        public override MediaClass GetMediaByID(object ID)
        {
            logger.Log("Entering RottenTomatosProvider.GetMediaByID(" + ID + ")");
            if ((!(ID is int)) || (!(ID is Int16)))
            {
                throw new ArgumentException("RottenTomatosProvider.GetMediaByID requires an 'int' ID");
            }

            var returnCode = new MediaClass(base.config);

            logger.Log("Exiting RottenTomatosProvider.GetMediaByID(" + ID.ToString() + ")");
            return returnCode;
        }

        public override MediaClass GetUpdateForMedia(MediaClass inputMedia, MediaMergeType MergeType)
        {
            throw new NotImplementedException();
        }

        public override VersionInfo GetVersionInfo()
        {
            throw new NotImplementedException();
        }

        public override bool Run()
        {
            throw new NotImplementedException();
        }

        public override List<MediaSearchReturn> SearchForMedia(MediaClass mediaObject)
        {
            throw new NotImplementedException();
        }

        public override List<MediaSearchReturn> SearchForMedia(MediaClass mediaObject, int maxReturns)
        {
            throw new NotImplementedException();
        }

        public override List<MediaSearchReturn> SearchForMediaByName(string name)
        {
            throw new NotImplementedException();
        }

        public override List<MediaSearchReturn> SearchForMediaByName(string name, int maxReturns)
        {
            throw new NotImplementedException();
        }

        public override List<MediaSearchReturn> SearchForMediaByNameAndYear(string name, int year)
        {
            throw new NotImplementedException();
        }

        public override List<MediaSearchReturn> SearchForMediaByNameAndYear(string name, int year, int maxReturns)
        {
            throw new NotImplementedException();
        }

        public override void SetDefaultConfig()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///     Simple method to merge the results from our query from the RottenTomatos movie to the inputMedia
        ///     using the MergeType defined in GetMedia
        /// </summary>
        /// <param name="inputMedia"> </param>
        /// <param name="MergeType"> </param>
        /// <param name="movie"> </param>
        /// <returns> </returns>
        private MediaClass MergeResults(MediaClass inputMedia, MediaMergeType MergeType, Movie movie)
        {
            logger.Log("Entering RottenTomatosProvider.MergeResults()");
            inputMedia.RottenTomatosID =
                (int)Util.CopyObject(movie.RottenTomatoesId, inputMedia.RottenTomatosID, MergeType);
            inputMedia.Title = (string)Util.CopyObject(movie.Title, inputMedia.Title, MergeType);
            if (movie.ReleaseDates.Count > 0)
            {
                DateTime releaseDate = movie.ReleaseDates[0].Date;
                inputMedia.Released = (DateTime)Util.CopyObject(releaseDate, inputMedia.Released, MergeType);
            }
            inputMedia.MPAARating = (string)Util.CopyObject(movie.MpaaRating, inputMedia.MPAARating, MergeType);
            if (movie.Runtime != null)
            {
                var runtime = new TimeSpan(0, (int)movie.Runtime, 0);
                Util.CopyObject(runtime, inputMedia.RunTime, MergeType);
            }

            logger.Log("Exiting RottenTomatosProvider.MergeResults()");
            return inputMedia;
        }

        #endregion Methods
    }
}